﻿Imports System.Data.OleDb
Public Class frmTinhThanh
#Region "Form Event"
    Private RowIndex As Integer = 0
    Private TinhThanhId_Edit As Integer = -1
    Private Sub frmTinhThanh_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            lblEmpty.Visible = False
            txtTenTinhThanh.Text = ""
            TinhThanh_Load()
        Catch ex As Exception
            ShowError()
        End Try
    End Sub
#End Region
#Region "Button Event"
    Private Sub cmdThemTinhThanh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdThemTinhThanh.Click
        Try
            If txtTenTinhThanh.Text = "" Then
                lblEmpty.Visible = True
                Exit Sub
            Else
                lblEmpty.Visible = False
            End If
            Dim dataAccess As New DataAccess
            Dim dbConn As New OleDb.OleDbConnection
            Dim dbCommand As OleDbCommand
            dataAccess.ConnectDatabase_DBAccess(dbConn)
            Dim query As String = ""
            If TinhThanhId_Edit = -1 Then
                'add new
                Dim MaTinhThanh As String = AutoGenKey("TinhThanh", 4)
                query = "Insert Into TinhThanh(MaTinhThanh,TenTinhThanh) Values(N'" & MaTinhThanh & "',N'" & txtTenTinhThanh.Text & "')"
            Else
                query = "Update TinhThanh Set TenTinhThanh=N'" & txtTenTinhThanh.Text & "' Where TinhThanhId=" & TinhThanhId_Edit
            End If
            dbCommand = New OleDbCommand(query, dbConn)
            dbCommand.ExecuteNonQuery()
            dbConn.Close()
            txtTenTinhThanh.Text = ""
            TinhThanh_Load()
        Catch ex As Exception
            ShowError()
        End Try
    End Sub

    Private Sub dgvTinhThanh_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvTinhThanh.CellClick
        Try
            RowIndex = e.RowIndex
        Catch ex As Exception
            ShowError()
        End Try
    End Sub

    Private Sub MenuChinhSua_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuChinhSua.Click
        Try
            TinhThanhId_Edit = dgvTinhThanh.Rows(RowIndex).Cells("TinhThanhId").Value
            txtTenTinhThanh.Text = dgvTinhThanh.Rows(RowIndex).Cells("TenTinhThanh").Value
            RowIndex = 0
        Catch ex As Exception
            ShowError()
        End Try
    End Sub

    Private Sub Xoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Xoa.Click
        Try
            Dim TinhThanhId As Integer = dgvTinhThanh.Rows(RowIndex).Cells("TinhThanhId").Value
            Dim TenTinhThanh As String = dgvTinhThanh.Rows(RowIndex).Cells("TenTinhThanh").Value
            Dim dlgRes As DialogResult
            dlgRes = MessageBox.Show( _
                  "Bạn muốn xóa  : " & TenTinhThanh, _
                  "Xóa Tỉnh/Thành phố", _
            MessageBoxButtons.OKCancel, _
                  MessageBoxIcon.Question)
            If dlgRes = DialogResult.OK Then
                Dim connectAccess As New DataAccess
                Dim dbConn As New OleDb.OleDbConnection
                Dim dbCommand As OleDbCommand
                Dim query As String = "Delete from TinhThanh Where TinhThanhId=" & TinhThanhId
                connectAccess.ConnectDatabase_DBAccess(dbConn)
                dbCommand = New OleDbCommand(query, dbConn)
                dbCommand.ExecuteNonQuery()
                dbConn.Close()
                TinhThanh_Load()
            End If
        Catch ex As Exception
            ShowError()
        End Try
    End Sub
#End Region
#Region "Load Data"
    Private Sub TinhThanh_Load()
        Try
            Dim dbConn As New OleDb.OleDbConnection
            Dim dataAccess As New DataAccess
            Dim dsTinhThanh As New DataSet
            Dim dtTinhThanh As New DataTable
            Dim query As String = "Select TinhThanhId,TenTinhThanh From TinhThanh Order By TenTinhThanh ASC"
            Dim adapter As OleDb.OleDbDataAdapter
            dataAccess.ConnectDatabase_DBAccess(dbConn)
            adapter = New OleDb.OleDbDataAdapter(query, dbConn)
            adapter.Fill(dsTinhThanh)
            dbConn.Close()
            dtTinhThanh = dsTinhThanh.Tables(0)
            dtTinhThanh.Columns.Add("STT")
            For i As Integer = 0 To dtTinhThanh.Rows.Count - 1
                dtTinhThanh.Rows(i)("STT") = i + 1
            Next
            dgvTinhThanh.AutoGenerateColumns = False
            dgvTinhThanh.DataSource = dtTinhThanh
        Catch ex As Exception
            ShowError()
        End Try
    End Sub
#End Region



End Class